<?php

/**
 *  
 */
class AdminBlogController extends AdminBaseController
{
    public $blog = null;

    /**
     *  before ...
     */
    public function beforeAction($action)
    {
        parent::beforeAction($action);

        $blog = $this->serviceManager->getFocusBlog();
        if( !$blog ) {
            Request::redirect('admin/');
        }

        $this->blog = $blog;
        $this->menus->setFocusByAction('adminBlog/setting');
        return true;
    }

    /**
     *  index
     */
    public function actionIndex()
    {
        Request::redirect('setting');
    }

    /**
     *  myself setting
     */
    public function actionSetting()
    {
        $blog = $this->blog;

        // update
        if( Request::isPost() ) {
            $posts = Request::getPostInfo();
            $blog->setName        ( $posts['name']        );
            $blog->setDescription ( $posts['description'] );
            $blog->setUpdateDate  ( time() );

            $messages = $blog->validate();
            $blog->filter();
            if( $messages ) {
                SessionManager::setErrorMessages('更新失敗');
                $data['fieldsMessages'] = $messages;
                $data['blog'] = $blog;
                $this->render('setting', $data );
                return;
            }
            $blogs = new Blogs();
            $blogs->updateBlog($blog);
            SessionManager::setSuccessMessages('更新成功');
        }

        // setting
        $data['blog'] = $blog;
        $this->render('setting',$data);
    }

}
